import VueRouter from 'vue-router'
import Router from 'vue-router'
import bookList from '../views/book/bookList'
import addBook from '../views/book/addBook'
import index from '../views/layout/index'
import Update from "../views/book/UpdateBook";
import About from "../views/info/About";
import login from "../views/login";
import errorPage from "../views/error/error"
import categoryList from "../views/category/categoryList";
import {getToken} from "../store/session";
import Vue from 'vue'
import borrows from "../views/borrow/borrows";
import profile from "../views/profile/index"
import dash from "../views/dashboard/index"
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题:Avoided redundant navigation to current location: “/XXX“
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

/*设置vue路由映射*/
/*第一层component填充app.vue中的router view*/
const routes = [
    //下面的这四个对应导航栏的四个模块，其中的children则是子菜单
    //因为第一层的component index用于填充App.vue,而children则填充layout/index.vue
    //最好不要乱改path，要在store/*.js 下更新常量
    {
        name: 'admin',
        component: index,
        path: '/',
        redirect: '/dashBoard',
        menus: ['首页', '图书管理', '分类管理', '借阅管理', '相关信息'], /*自定义属性，admin后台的菜单项名*/
        icons: ['el-icon-s-promotion', 'el-icon-s-management', 'el-icon-s-operation', 'el-icon-star-on', 'el-icon-s-comment'], /*对应的图标*/
        pathIndex: [[0], [1, 2, 3], [4], [5], [6]], /*每个菜单的子菜单在children中对应的index*/
        children: [
            {
                path: '/dashBoard',
                name: '首页',
                meta: {title: "dashBoard"},
                component: dash
            },
            {
                path: '/book/list',
                name: '查询图书',
                meta: {title: "图书列表"}, //页面标题，在路由守卫设置
                component: bookList
            },
            {
                path: '/book/addBook',
                name: '新增图书',
                meta: {title: "添加图书"},
                component: addBook
            },
            {
                hide: true, //不显示，而是通过某个入口跳转
                path: '/book/updateBook',
                name: '修改图书',
                meta: {title: "修改图书"},
                component: Update
            },
            {
                path: '/category/list', meta: {title: "分类列表"},
                component: categoryList,
                name: '分类列表'
            },
            {
                path: '/borrow/list',
                name: '借阅列表',
                meta: {title: "管理个人借阅"},
                component: borrows
            },
            {
                path: '/about/readme',
                name: 'about',
                component: About
            },
            {
                path: '/profile',
                name: '管理员信息',
                component: profile,
                meta: {title: "profile"},
            },

        ]
    },
    {
        path: '/login',
        meta: {title: 'login'},
        component: login,
        name: 'login'
    },
    {
        path: '*',
        component: errorPage
    }
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {//beforeEach是router的钩子函数，在进入路由前执行
    if (getToken() != null) {
        if (to.path === '/login')
            next('/');
        else
            next();
    } else {
        if (to.path === '/login')
            next();
        else
            next('/login')
    }
});

export default router
